package arayuz;

import motor.Matematik;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class ArayuzMain {
	private static Text text;
	private static boolean sifirla = true;

	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setText("Calculatorix");
		shell.setLayout(new GridLayout(4, true));
		text = new Text(shell, SWT.SINGLE | SWT.BORDER);
		GridData tGridData = new GridData(GridData.FILL_HORIZONTAL);
		tGridData.widthHint = 200;
		tGridData.horizontalSpan = 4;
		text.setLayoutData(tGridData);

		int k = 0;
		for (int i = 0; i < 10; i++) {
			int j = (i + 1) % 10;
			makeButton(shell, j + "");
			if (j % 3 == 0) {
				switch (k) {
				case 0:
					makeButton(shell, "+");

					break;
				case 1:
					makeButton(shell, "-");
					break;
				case 2:
					makeButton(shell, "X");
					break;
				case 3:
					// make space for horizontal align for "%" button
					new Label(shell, SWT.None);
					new Label(shell, SWT.None);
					// ...............
					makeButton(shell, "%");
					break;

				default:
					break;
				}
				k++;
			}

		}

		Button esittir = new Button(shell, SWT.PUSH);
		esittir.setText("=");
		esittir.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				4, 1));
		esittir.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub

				String al = text.getText();
				al = al.trim();
				if (al.length() < 1)
					return; // boşsa salla gitsin
				Matematik mat = new Matematik();
				if (al.indexOf("+") > -1) {
					String[] sayilar = al.split("\\+");

					double x = Double.valueOf(sayilar[0]);
					double y = Double.valueOf(sayilar[1]);
					double son = mat.topla(x, y);
					text.setText("Sonuç: " + son);
				} else if (al.indexOf("X") > -1) {
					String[] sayilar = al.split("X");

					double x = Double.valueOf(sayilar[0]);
					double y = Double.valueOf(sayilar[1]);
					double son = mat.carp(x, y);
					text.setText("Sonuç: " + son);
				} else if (al.indexOf("%") > -1) {
					String[] sayilar = al.split("%");

					double x = Double.valueOf(sayilar[0]);
					double y = Double.valueOf(sayilar[1]);
					double son = mat.bol(x, y);
					text.setText("Sonuç: " + son);

				} else if (al.indexOf("-") > -1) {
					String[] sayilar = al.split("-");

					double x = Double.valueOf(sayilar[0]);
					double y = Double.valueOf(sayilar[1]);
					double son = mat.cikar(x, y);
					text.setText("Sonuç: " + son);
				}

				sifirla = true;

			}

		});

		shell.pack();
		shell.open();
		while (!shell.isDisposed())
			if (!display.readAndDispatch())
				display.sleep();
		display.dispose();
	}

	public static void makeButton(Shell shell, String name) {
		Button butNumber = new Button(shell, SWT.PUSH);
		butNumber.setLayoutData(new GridData(GridData.FILL_BOTH));
		butNumber.setText(name);
		butNumber.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				if (sifirla) {
					text.setText("");
					sifirla = false;
				}
				text.setText((text.getText() + ((Button) e.widget).getText()));
			}
		});
	}

}
